<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>弹簧动画阻尼效果演示</title>
    <script type="module">
        import { animate, spring } from 'https://esm.sh/animejs';
        
        // 页面加载完成后执行动画
        document.addEventListener('DOMContentLoaded', () => {
            // 为每个方块添加点击事件重新触发动画
            document.querySelectorAll('.square').forEach(square => {
                square.addEventListener('click', function() {
                    const stiffness = parseInt(this.dataset.stiffness);
                    const damping = parseInt(this.dataset.damping);
                    
                    // 重置位置
                    this.style.transform = 'translateX(0) rotate(0deg)';
                    
                    // 应用弹簧动画
                    animate(this, {
                        x: '17rem',
                        rotate: 360,
                        duration: 2000,
                        ease: spring({ stiffness, damping })
                    });
                });
            });
            
            // 初始动画
            setTimeout(animateAll, 500);
            
            // 重置按钮
            document.getElementById('resetBtn').addEventListener('click', () => {
                document.querySelectorAll('.square').forEach(square => {
                    square.style.transform = 'translateX(0) rotate(0deg)';
                });
                setTimeout(animateAll, 300);
            });
            
            function animateAll() {
                document.querySelectorAll('.square').forEach(square => {
                    const stiffness = parseInt(square.dataset.stiffness);
                    const damping = parseInt(square.dataset.damping);
                    
                    animate(square, {
                        x: '17rem',
                        rotate: 360,
                        duration: 2000,
                        ease: spring({ stiffness, damping })
                    });
                });
            }
        });
    </script>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }
        
        body {
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            background: linear-gradient(135deg, #1a2a6c, #b21f1f, #fdbb2d);
            color: white;
            min-height: 100vh;
            padding: 2rem;
        }
        
        .container {
            max-width: 900px;
            margin: 0 auto;
            background: rgba(0, 0, 0, 0.7);
            border-radius: 15px;
            padding: 2rem;
            box-shadow: 0 10px 30px rgba(0, 0, 0, 0.5);
        }
        
        h1 {
            text-align: center;
            margin-bottom: 1rem;
            font-size: 2.5rem;
            text-shadow: 0 2px 5px rgba(0, 0, 0, 0.5);
        }
        
        .description {
            text-align: center;
            margin-bottom: 2rem;
            line-height: 1.6;
            font-size: 1.1rem;
            color: #e0e0e0;
        }
        
        .controls {
            display: flex;
            justify-content: center;
            margin-bottom: 2rem;
        }
        
        button {
            background: #4a00e0;
            color: white;
            border: none;
            padding: 0.8rem 1.5rem;
            border-radius: 50px;
            font-size: 1rem;
            cursor: pointer;
            transition: all 0.3s ease;
            box-shadow: 0 4px 10px rgba(0, 0, 0, 0.3);
        }
        
        button:hover {
            background: #8e2de2;
            transform: translateY(-2px);
            box-shadow: 0 6px 15px rgba(0, 0, 0, 0.4);
        }
        
        .demo-area {
            display: flex;
            flex-direction: column;
            gap: 2rem;
        }
        
        .demo-row {
            background: rgba(255, 255, 255, 0.1);
            border-radius: 10px;
            padding: 1.5rem;
            transition: all 0.3s ease;
        }
        
        .demo-row:hover {
            background: rgba(255, 255, 255, 0.15);
            transform: translateY(-3px);
        }
        
        .row-title {
            margin-bottom: 1rem;
            font-size: 1.3rem;
            display: flex;
            align-items: center;
            gap: 10px;
        }
        
        .row-title .param {
            font-size: 0.9rem;
            background: rgba(255, 255, 255, 0.2);
            padding: 0.3rem 0.8rem;
            border-radius: 20px;
        }
        
        .animation-track {
            height: 100px;
            background: rgba(255, 255, 255, 0.05);
            border-radius: 8px;
            position: relative;
            overflow: hidden;
            border: 1px solid rgba(255, 255, 255, 0.1);
        }
        
        .square {
            width: 80px;
            height: 80px;
            border-radius: 10px;
            position: absolute;
            left: 20px;
            top: 10px;
            cursor: pointer;
            display: flex;
            align-items: center;
            justify-content: center;
            font-weight: bold;
            box-shadow: 0 5px 15px rgba(0, 0, 0, 0.3);
            transition: transform 0.2s;
        }
        
        .square:hover {
            transform: scale(1.05);
        }
        
        .square-1 {
            background: linear-gradient(135deg, #ff9a9e, #fad0c4);
            color: #333;
        }
        
        .square-2 {
            background: linear-gradient(135deg, #a1c4fd, #c2e9fb);
            color: #333;
        }
        
        .square-3 {
            background: linear-gradient(135deg, #ffecd2, #fcb69f);
            color: #333;
        }
        
        .square-4 {
            background: linear-gradient(135deg, #84fab0, #8fd3f4);
            color: #333;
        }
        
        .damping-explanation {
            margin-top: 2rem;
            background: rgba(255, 255, 255, 0.1);
            border-radius: 10px;
            padding: 1.5rem;
        }
        
        .damping-explanation h2 {
            margin-bottom: 1rem;
            color: #8fd3f4;
        }
        
        .damping-explanation ul {
            padding-left: 1.5rem;
            line-height: 1.6;
        }
        
        .damping-explanation li {
            margin-bottom: 0.5rem;
        }
        
        .footer {
            text-align: center;
            margin-top: 2rem;
            color: #b0b0b0;
            font-size: 0.9rem;
        }
        
        @media (max-width: 768px) {
            .container {
                padding: 1rem;
            }
            
            h1 {
                font-size: 2rem;
            }
            
            .square {
                width: 60px;
                height: 60px;
            }
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>弹簧动画阻尼效果演示</h1>
        <p class="description">点击任意方块重新触发其动画，观察不同阻尼值对弹簧效果的影响</p>
        
        <div class="controls">
            <button id="resetBtn">重置所有动画</button>
        </div>
        
        <div class="demo-area">
            <div class="demo-row">
                <div class="row-title">
                    <span>低阻尼 (5)</span>
                    <span class="param">stiffness: 70, damping: 5</span>
                </div>
                <div class="animation-track">
                    <div class="square square-1" data-stiffness="70" data-damping="5">低阻尼</div>
                </div>
            </div>
            
            <div class="demo-row">
                <div class="row-title">
                    <span>中等阻尼 (15)</span>
                    <span class="param">stiffness: 70, damping: 15</span>
                </div>
                <div class="animation-track">
                    <div class="square square-2" data-stiffness="70" data-damping="15">中阻尼</div>
                </div>
            </div>
            
            <div class="demo-row">
                <div class="row-title">
                    <span>高阻尼 (30)</span>
                    <span class="param">stiffness: 70, damping: 30</span>
                </div>
                <div class="animation-track">
                    <div class="square square-3" data-stiffness="70" data-damping="30">高阻尼</div>
                </div>
            </div>
            
            <div class="demo-row">
                <div class="row-title">
                    <span>非常高阻尼 (50)</span>
                    <span class="param">stiffness: 70, damping: 50</span>
                </div>
                <div class="animation-track">
                    <div class="square square-4" data-stiffness="70" data-damping="50">超高阻尼</div>
                </div>
            </div>
        </div>
        
        <div class="damping-explanation">
            <h2>阻尼参数说明</h2>
            <ul>
                <li><strong>低阻尼 (5)</strong> - 弹簧会长时间振动，需要较长时间才能稳定下来</li>
                <li><strong>中等阻尼 (15)</strong> - 适度的振动，在合理时间内稳定</li>
                <li><strong>高阻尼 (30)</strong> - 轻微振动，快速稳定到目标位置</li>
                <li><strong>非常高阻尼 (50)</strong> - 几乎没有振动，类似于常规缓动效果</li>
            </ul>
            <p style="margin-top: 1rem;">阻尼值控制弹簧系统的能量损失速度。阻尼越高，弹簧振动停止得越快。</p>
        </div>
        
        <div class="footer">
            <p>使用 Anime.js 弹簧物理动画 | 点击方块重新触发动画</p>
        </div>
    </div>
</body>
</html>